package com.zenith.edu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zenith.edu.mapper.EduWarningRuleMapper;
import com.zenith.edu.po.domain.EduWarningRule;
import com.zenith.edu.po.dto.EduWarningRuleDTO;
import com.zenith.edu.po.vo.EduWarningRuleVO;
import com.zenith.edu.service.EduWarningRuleService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class EduWarningRuleServiceImpl extends ServiceImpl<EduWarningRuleMapper, EduWarningRule> implements EduWarningRuleService {
    
    @Override
    public EduWarningRuleVO createWarningRule(EduWarningRuleDTO dto) {
        EduWarningRule warningRule = new EduWarningRule();
        BeanUtils.copyProperties(dto, warningRule);
        this.save(warningRule);
        
        EduWarningRuleVO vo = new EduWarningRuleVO();
        BeanUtils.copyProperties(warningRule, vo);
        return vo;
    }
    
    @Override
    public EduWarningRuleVO getWarningRuleById(Long id) {
        EduWarningRule warningRule = this.getById(id);
        if (warningRule == null) {
            return null;
        }
        
        EduWarningRuleVO vo = new EduWarningRuleVO();
        BeanUtils.copyProperties(warningRule, vo);
        return vo;
    }
    
    @Override
    public EduWarningRuleVO updateWarningRule(EduWarningRuleDTO dto) {
        EduWarningRule warningRule = new EduWarningRule();
        BeanUtils.copyProperties(dto, warningRule);
        this.updateById(warningRule);
        
        EduWarningRuleVO vo = new EduWarningRuleVO();
        BeanUtils.copyProperties(warningRule, vo);
        return vo;
    }
    
    @Override
    public boolean deleteWarningRule(Long id) {
        return this.removeById(id);
    }
    
    @Override
    public List<EduWarningRuleVO> getWarningRuleList() {
        List<EduWarningRule> warningRuleList = this.list();
        return warningRuleList.stream().map(warningRule -> {
            EduWarningRuleVO vo = new EduWarningRuleVO();
            BeanUtils.copyProperties(warningRule, vo);
            return vo;
        }).collect(Collectors.toList());
    }
}